//
//  MKMainViewController.m
//  ManKeCartoon
//
//  Created by 邹功梁 on 15/10/16.
//  Copyright © 2015年 ManKe. All rights reserved.
//

#import "MKMainViewController.h"

@interface MKMainViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation MKMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64-49) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.bounces = NO;
    _tableView.rowHeight = 60;
    [self.view addSubview:_tableView];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/3.)];
    
    [_headImageView setBackgroundColor:[UIColor greenColor]];
    [_tableView setTableHeaderView:_headImageView];
    [_headImageView setImage:[UIImage imageNamed:@"469401cf1727f8144309e4898fc05857.jpg"]];
    
    _iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _iconButton.frame = CGRectMake(0, 0, _headImageView.frame.size.height/2., _headImageView.frame.size.height/2.);
    _iconButton.center = _headImageView.center;
    [_iconButton setBackgroundColor:[UIColor redColor]];
    [_headImageView addSubview:_iconButton];
    _iconButton.layer.cornerRadius = _iconButton.frame.size.height/2.;
    _iconButton.layer.masksToBounds = YES;
    [_iconButton setImage:[UIImage imageNamed:@"f73c2c3e75f90103dd0761e94903db46.jpg"] forState:UIControlStateNormal];
    
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return 3;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *mainTableViewCellId = @"mainTableViewCell";
    
    MKMainTableViewCell *mainTableViewCell = [tableView dequeueReusableCellWithIdentifier:mainTableViewCellId];
    
    if (mainTableViewCell == nil) {
        
        mainTableViewCell = [[MKMainTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:mainTableViewCellId];
    }
    
    NSArray *titleArray = @[@"意见反馈", @"清除缓存", @"关于我们"];
    NSArray *iconArray = @[@"iconfont-yijianfankui.png", @"iconfont-lajitong.png", @"iconfont-guanyuwomenfuzhi.png"];
    [mainTableViewCell.titleLabel setText:titleArray[indexPath.row]];
    [mainTableViewCell.iconImageView setImage:[UIImage imageNamed:iconArray[indexPath.row]]];
    [mainTableViewCell.iconImageView setContentMode:UIViewContentModeScaleAspectFit];
    return mainTableViewCell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 1) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"清除缓存" message:@"确认清除缓存吗?" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
           
            [[SDImageCache sharedImageCache] clearDisk];
            [URLCache resetCache];
            
            NSDictionary *dictionary = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
            for(NSString* key in [dictionary allKeys]){
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }

            UIAlertController *alertControllerPlus = [UIAlertController alertControllerWithTitle:nil message:@"缓存清除成功" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alertControllerPlus animated:YES completion:^{
                [self dismissViewControllerAnimated:alertControllerPlus completion:nil];
            }];
            
        }];
        [alertController addAction: cancelAction];
        [alertController addAction: confirmAction];
        [self presentViewController:alertController animated:YES completion:nil];
        
    } else if (indexPath.row == 2) {
        
        MKAboutUsViewController *aboutUsViewController = [[MKAboutUsViewController alloc] init];
        aboutUsViewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:aboutUsViewController animated:YES];
        
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
